package hifiSpeakers;

/**
 *@author Adam
 */

import javax.jmdns.JmDNS;

import java.net.PortUnreachableException;
import java.util.Random;
import javax.jmdns.ServiceInfo;

public class BonjourService{

	public static final String SERVICE_TYPE = "uss.local.";
	public String SERVICE_NAME = "local_Hifi_Service"+randomNumber();
	public int SERVICE_PORT = 0;
	JmDNS jmdns;
	ServiceInfo info;
	String RMIName = "";

	public int randomNumber(){
		Double randNumber = Math.random() * 2000 + 10000;
		int p = randNumber.intValue();
		return p;
	}
	
	
	public BonjourService(String myRMIName, int port){
		RMIName = myRMIName;
		SERVICE_PORT = port;
		System.out.println("got port from socket: " + SERVICE_PORT);
	}
	

	public void registerService() {
		try{
			jmdns = JmDNS.create();
			info = ServiceInfo.create(SERVICE_TYPE, SERVICE_NAME, SERVICE_PORT, 0, 0,"a_property=" + RMIName);
			jmdns.registerService(info);
			System.out.println("Registered Service as " + info);

		}
		catch(Exception e){
			System.out.println("problem registering BonjourListenService");
		}
	}

	public void unregisterService() {
		jmdns.unregisterService(info);
		jmdns.close();
	}

}
